home *** CD-ROM | disk | FTP | other *** search
/ MIDICraft's MIDINET CD-ROM / MIDICraft's MIDINET CD-ROM.iso / DOSUTILS / KORG / RECVSYX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-29  |  3.5 KB  |  193 lines

  1. // recvsyx: record data from midi input
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include "sb.hpp"
  8. #include "filei3.hpp"
  9.  
  10. char filename[128] = "recv.syx";
  11. Soundcard* card = 0;
  12. FILE* f = 0;
  13.  
  14. int hex = 0;
  15. int sysex_only = 0;
  16. int echo = 0;
  17.  
  18. unsigned char buf[32000];
  19. int buflen = 0;
  20. long skip = 0;
  21.  
  22. void flushbuf()
  23. {
  24.   if (buflen > 0)
  25.   {
  26.     if (hex)
  27.     {
  28.     static int col = 0;
  29.  
  30.       for (int i = 0; i < buflen; i++)
  31.       {
  32.     if (col >= 25 || (buf[i] >= 0x80 && buf[i] != 0xf8 && buf[i] != 0xfe && buf[i] != 0xf7))
  33.     {
  34.       fputc('\r', f);
  35.       fputc('\n', f);
  36.       col = 0;
  37.     }
  38.     fprintf(f, "%02X ", buf[i]);
  39.     col++;
  40.       }
  41.     }
  42.     else
  43.     {
  44.       if (fwrite(buf, buflen, 1, f) != 1)
  45.     fprintf(stderr, "write error\n");
  46.     }
  47.   }
  48.  
  49.   buflen = 0;
  50. }
  51.  
  52. void add(FILE* f, unsigned char* s, int len)
  53. {
  54.   if (buflen + 7 > sizeof(buf))
  55.     flushbuf();
  56.   memcpy(buf+buflen, s, len);
  57.   if (len > 0)
  58.     buflen += len;
  59. }
  60.  
  61. int receive_syx()
  62. {
  63.   if (!card->reset())
  64.     fprintf(stderr, "reset command failed\n");
  65.   unsigned char c;
  66.   int ret = 0;
  67.  
  68.   fprintf(stderr, "recording...\n");
  69.   card->startinput();
  70.  
  71.   f = fopen(filename, "wb");
  72.   if (!f)
  73.   {
  74.     perror(filename);
  75.     return 0;
  76.   }
  77.   int online = 0;
  78.   int insysex = 0;
  79.  
  80.   while (1)
  81.   {
  82.     int count = card->hear(&c, 1);
  83.     if (count <= 0)
  84.     {
  85.       if (kbhit() && getch() == 27)
  86.     break;
  87.       continue;
  88.     }
  89.     if (c == 0xf8 || c == 0xfe)
  90.     {
  91.       if (online == 0)
  92.     fprintf(stderr,"*\b");
  93.       else if (online == 12)
  94.     fprintf(stderr,".\b");
  95.       online++;
  96.       if (online == 24)
  97.       {
  98.     if (kbhit() && getch() == 27)
  99.       break;
  100.     online = 0;
  101.       }
  102.     }
  103.     if (insysex)
  104.     {
  105.       if (c == 0xf7)
  106.       {
  107.     insysex = 0;
  108.       }
  109.     }
  110.     else
  111.     {
  112.       if (c == 0xf0)
  113.       {
  114.     insysex = 1;
  115.       }
  116.       else if (sysex_only)
  117.     continue;
  118.     }
  119.  
  120.     add(f, &c, 1);
  121.     if (echo)
  122.     {
  123.       printf(" %02X", c);
  124.       if (c == 0xF7)
  125.     putchar('\n');
  126.     }
  127.     ret = 1;
  128.   }
  129.   if (echo)
  130.     printf("\n");
  131.   card->stopinput();
  132.   if (f)
  133.   {
  134.     flushbuf();
  135.     fclose(f); f = 0;
  136.   }
  137.   return ret;
  138. }
  139.  
  140. int main(int argc, char** argv)
  141. {
  142.   argc--; argv++;
  143.   while (argc > 0 && **argv == '-')
  144.   {
  145.     if (strnicmp(*argv, "-hex", 4) == 0)
  146.     {
  147.       hex = 1;
  148.       argc--; argv++;
  149.       continue;
  150.     }
  151.     if (strnicmp(*argv, "-h", 2) == 0)
  152.     {
  153.       printf("usage: recvsyx [-h] [-hex] [-echo] [-sysex] [filename]\n");
  154.       printf("-hex\tsave received data to text file (default: binary file)\n");
  155.       printf("-echo\techo the received data (hexadecimal)\n");
  156.       printf("-sysex\tsave only sysex messages (F0 ... F7)\n");
  157.       printf("filename\tsave data to filename (default: recv.syx)\n");
  158.       return 1;
  159.     }
  160.     if (strnicmp(*argv, "-echo", 2) == 0)
  161.     {
  162.       echo = 1;
  163.       argc--; argv++;
  164.       continue;
  165.     }
  166.     if (strnicmp(*argv, "-sysex", 2) == 0)
  167.     {
  168.       sysex_only = 1;
  169.       argc--; argv++;
  170.       continue;
  171.     }
  172.     fprintf(stderr, "invalid option %s\n", *argv);
  173.     argc--; argv++;
  174.   }
  175.   if (argc > 0)
  176.   {
  177.     strcpy(filename, *argv);
  178.   }
  179.   card = detect_soundcard();
  180.   if (!card)
  181.   {
  182.     fprintf(stderr, "Could not detect soundcard\n");
  183.     return 1;
  184.   }
  185.  
  186.   if (!receive_syx())
  187.     fprintf(stderr, "no response from soundcard\n");
  188.   else
  189.     fprintf(stderr, "data written to file %s\n", filename);
  190.   delete card;
  191.   return 0;
  192. }
  193.